package com.huatai.exchange.bootup.controller;

import com.faneui.bootup.controller.AbstractRestController;
import com.faneui.common.bean.BaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.BeanMapper;
import com.faneui.common.utils.DateFormatUtils;
import com.faneui.common.utils.ListUtils;
import com.huatai.exchange.dal.model.ExcFileLoadInfo;
import com.huatai.exchange.facade.ExcFileLoadInfoController;
import com.huatai.exchange.facade.bean.FileLoadInfoVo;
import com.huatai.exchange.facade.bean.QueryFileLoadInfoVoListResp;
import com.huatai.exchange.service.ExcFileLoadInfoService;
import com.huatai.exchange.service.ExcuteExchangeService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "loadinfo")
@Api("公共文件备份信息")
public class FileLoadInfoControllerImpl extends AbstractRestController implements ExcFileLoadInfoController {

    @Autowired
    private ExcFileLoadInfoService excFileLoadInfoService;

    @Autowired
    private ExcuteExchangeService excuteExchangeService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public QueryFileLoadInfoVoListResp queryFileLoadInfoVoList(@ModelAttribute FileLoadInfoVo fileLoadInfoVo) throws Exception {
        QueryFileLoadInfoVoListResp resp = new QueryFileLoadInfoVoListResp();
        try {
            ExcFileLoadInfo param = BeanMapper.map(fileLoadInfoVo, ExcFileLoadInfo.class);
            List<ExcFileLoadInfo> records = excFileLoadInfoService.queryExcFileLoadInfoList(param);
            if (ListUtils.isNotEmpty(records)) {
                List<FileLoadInfoVo> vos = BeanMapper.mapList(records, FileLoadInfoVo.class);
                resp.setFileLoadInfoVos(vos);
            }
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "/load/{fileId}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResp loadThirdFile(@PathVariable String fileId, @RequestParam(required = false) String occurDate) throws BusinessException {
        BaseResp resp = new BaseResp();
        if (StringUtils.isBlank(occurDate)) {
            occurDate = DateFormatUtils.yyyyMMdd.format(new Date());
        }
        try {
            excuteExchangeService.executeLoadDataFile(fileId, occurDate);
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }


    @RequestMapping(value = "/load/supply/{fileId}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResp suppyLoadThirdFile(@PathVariable String fileId,
                                       @RequestParam Integer startDate,
                                       @RequestParam Integer endDate) throws BusinessException {
        BaseResp resp = new BaseResp();
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(DateFormatUtils.yyyyMMdd.parse(startDate + ""));
            while (startDate <= endDate) {
                startDate = Integer.valueOf(DateFormatUtils.yyyyMMdd.format(c.getTime())) ;
                excuteExchangeService.executeLoadDataFile(fileId, startDate.toString());
            }

        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }
}
